using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace QuadAnimation.Animation.XML
{
    /// <summary>
    /// Class responsible for loading animation data from XML.
    /// </summary>
    public class XMLAnimationLoader
    {
        public static void LoadAnimationSettings(String xmlFile, ref AnimationSettings settings, ContentManager content)
        {
            String textureName;
            XmlDocument document;
            String frameName = "idle";
            int frameStart = 0;
            int frameEnd = 0;
            float timePerFrame = 0f;

            //Load the document
            document = new XmlDocument();
            document.Load(xmlFile);

            //texture
            textureName = document.GetElementsByTagName("texture").Item(0).InnerText;
            settings.texture = content.Load<Texture2D>(@"Data\Images\" + textureName);

            //texture width
            settings.textureWidth = settings.texture.Width;

            //texture height
            settings.textureHeight = settings.texture.Height;

            //number of rows
            settings.numOfRows = Int32.Parse(document.GetElementsByTagName("numberOfRows").Item(0).InnerText);

            //frames per row
            settings.framesPerRow = Int32.Parse(document.GetElementsByTagName("framesPerRow").Item(0).InnerText);

            //animation width
            settings.animationWidth = Int32.Parse(document.GetElementsByTagName("animationWidth").Item(0).InnerText);

            //animation height
            settings.animationHeight = Int32.Parse(document.GetElementsByTagName("animationHeight").Item(0).InnerText);

            //frames
            XmlNodeList frames = document.GetElementsByTagName("frame");
            settings.frames = new Dictionary<string, AnimationFrame>();

            foreach (XmlNode frame in frames)
            {
                AnimationFrame animationFrame;
                XmlNodeList frameChildren = frame.ChildNodes;

                foreach (XmlNode child in frameChildren)
                {
                    if (child.Name.Equals("name"))
                    {
                        frameName = child.InnerText;
                    }
                    else if (child.Name.Equals("start"))
                    {
                        frameStart = Int32.Parse(child.InnerText);
                    }
                    else if (child.Name.Equals("end"))
                    {
                        frameEnd = Int32.Parse(child.InnerText);
                    }
                    else if (child.Name.Equals("timePerFrame"))
                    {
                        timePerFrame = float.Parse(child.InnerText);
                    }
                }

                //Now add this frame to the list
                animationFrame = new AnimationFrame();
                animationFrame.start = frameStart;
                animationFrame.end = frameEnd;
                animationFrame.timePerFrame = timePerFrame;

                settings.frames.Add(frameName, animationFrame);
            }
        }
    }
}
